import { ReactElement, useRef, useState } from 'react'
import Search, { formatCondition } from './components/search'
import style from './index.module.less'
import Table, { TableRef } from '@components/table'
import { getColumn } from './util'
import { awaitWrap } from '@js/tool'
import { getListRequest } from './ajax'
import { ArbitratorSchema } from '@components/schema/arbitrator'
import { Button } from 'antd'
import Detail from './components/detail'

// 初始化数据
async function initData (data: { [k: string]: any}, setDataSource: (v: ArbitratorSchema[]) => void, setTotal: (v: number) => void, setLoading: (v: boolean) => void): Promise<void> {
  setLoading(true)
  const [e, d] = await awaitWrap(getListRequest({
    condition: formatCondition(data),
    page: data.page,
    limit: data.limit
  }))
  setLoading(false)
  if (e === null && d !== null) {
    setTotal(d.total)
    setDataSource(d.list)
  }
}

function Main (): ReactElement {
  const tableRef = useRef<TableRef>(null)
  const [total, setTotal] = useState(0)
  const [loading, setLoading] = useState(false)
  const [dataSource, setDataSource] = useState<ArbitratorSchema[]>([])
  const [visible, setVisible] = useState(false)
  const [modifyData, setModifyData] = useState<ArbitratorSchema | null>(null)
  const [params, setParams] = useState<{[k: string]: any}>({})
  return (
    <div className={style.container}>
      <Detail visible={visible} value={modifyData} hide={() => { setModifyData(null); setVisible(false) }} />
      <Search params={params} setParams={setParams} />
      <div className={style.table}>
        {/* <div className={style.btns}>
          <Button type='primary' onClick={() => { setModifyData(null); setVisible(true) }}>添加仲裁员</Button>
        </div> */}
        <Table params={params} setParams={setParams} ref={tableRef} columns={getColumn(setVisible, setModifyData, () => tableRef.current?.refreshData())} loading={loading} total={total} dataSource={dataSource} onChange={async d => {
          await initData(d, setDataSource, setTotal, setLoading)
        }} />
      </div>
    </div>
  )
}

export default Main
